<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use think\Request;

class System extends Base
{
	/*轮播图添加*/
	public function lbt(){
		if(request()->isPost()){
			$file = request()->file('img_url');
			$data['img_url'] = $this->upload($file);
			Db('photo')->insert($data);
			$this->redirect('System/lbt');
		}else{
			return $this->fetch();
		}
		
	}


	/*支付方式*/
	public function payment_list(){
		return $this->fetch();
	}
	/*配送方式*/
	public function shipping_list(){
		return $this->fetch();
	}

	/*地区列表*/
	public function area_list(){
		$id = input('get.id');
		$id = isset($id)? $id :0;
 		$list = Db('region')->where('parent_id',$id)->select();
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function area_list_type(){
		$pid = input('request.id');
		$parent = Db('region')->where('region_id',$pid)->find();
		$list = Db('region')->where('parent_id',$pid)->select();
		$this->assign('parent',$parent);
		$this->assign('pid',$pid);
		$this->assign('list',$list);
		return $this->fetch();
	}

	/*ajax地区添加*/
	public function area_add(){
		$data = input('post.');
		$name = input('post.region_name');
		$result = Db('region')->insert($data);
		if($result){
			$arr = [
				'code'=>1,
				'data'=>$name
			];
		}else{
			$arr = [
				'code'=>0,
				'data'=>''
			];
		}
		echo json_encode($arr);
	}
	
	// /*一级地区列表添加*/
	// public function area_add(){
	// 	$data = input('post.');
	// 	$result = Db('region')->insert($data);
	// 	$this->redirect('System/area_list');
	// }

	// /*二三四级地区列表添加*/
	// public function area_addd(){
	// 	$data = input('post.');
	// 	$pid = $data['parent_id'];
	// 	$result = Db('region')->insert($data);
	// 	$this->redirect('System/area_list_type',['id'=>$pid]);
	// }
	
	/*地区列表删除*/
	public function area_del(){
		$id = input('get.id');
		$result = Db('region')->where('region_id',$id)->delete();
		if($result){
			$arr = [
				'code'=>1,
				'msg'=>'删除成功'
			];
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'删除失败'
			];
		}
		echo json_encode($arr);
	}


	/*友情链接*/
	public function friend_link_list(){
		$list = Db('friend_link')->paginate();
		$this->assign('list',$list);
		return $this->fetch();
	}

	/*友情链接添加*/
	public function link_add(){
		if(request()->isPost()){
			$data = input('post.');
			$file = request()->file('link_logo');
			$data['link_logo'] = $this->upload($file);
			$result = Db('friend_link')->insert($data);
			$this->redirect('System/friend_link_list');
		}else{
			return $this->fetch();
		}	
		
	}

	/*友情链接修改*/
	public function link_save(){
		$lname = input('get.lname');
		$id = input('get.id');
		$result = Db('friend_link')->where('link_id',$id)->update(['link_name' => $lname]);
		echo json_encode($result);
	}

	/*友情链接删除*/
	public function link_del(){
		$id = input('get.id');
		$result = db('friend_link')->where('link_id',$id)->delete();
		if($result){
			$arr = [
				'code'=>1,
				'msg'=>'删除成功'
			];
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'删除失败'
			];
		}
		echo json_encode($arr);
	}

	/*上传*/
	public function upload($file)
	{    
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	             $a=$info->getSaveName();
	             $imgp= str_replace("\\","/",$a);
	             $imgpath='/uploads/'.$imgp;
	       		
	            return $imgpath;
	          
	        }else{   
	            return $file->getError();
	        }
	    }
	}


}//结尾